from contextlib import asynccontextmanager

from fastapi import FastAPI
from starlette.staticfiles import StaticFiles

from API.CRequest import app_request
from fastapi.middleware.cors import CORSMiddleware
from API.medicine import medicine_router
from setting.database import Base, engine


@asynccontextmanager
async def lifespan(app: FastAPI) -> None:
    print("启动")
    Base.metadata.create_all(engine)
    yield


app = FastAPI(lifespan=lifespan)
app.include_router(
    router=app_request,
    prefix="/client",
)
app.include_router(medicine_router, prefix="/medicine")
app.mount("/media", StaticFiles(directory="media"), name="media")

# 解决跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "*"
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
def welcome():
    return {"message": "Welcome"}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        app="main:app",
        host='127.0.0.1',
        port=12345,
        reload=True,
        workers=14
    )
